//
//  SLMoreButton.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/25.
//  
  

#import "SLMoreButton.h"
#import <YYKit.h>
#import <Masonry.h>



@interface SLMoreButton ()
@property (nonatomic, strong) UIView *content;
@end
@implementation SLMoreButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _spacing = 10;
        [self addSubview:self.content];
        [self.content addSubview:self.titleLabel];
        [self.content addSubview:self.imageView];

        [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return self;
}

- (CGSize)intrinsicContentSize {
    [self layoutIfNeeded];
    return self.content.bounds.size;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    UIView *latestView = nil;
    if (_titleLabel.text.length > 0) {
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(_content);
            if (!_imageView.image) {
                make.right.equalTo(_content);
            }
        }];
        latestView = _titleLabel;
    }
    if (_imageView.image) {
        [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (latestView) {
                make.left.equalTo(latestView.mas_right).offset(_spacing);
            } else {
                make.left.equalTo(_content);
            }
            make.top.bottom.right.equalTo(_content);
        }];
    }
}


- (void)setSpacing:(CGFloat)spacing {
    _spacing = spacing;
    [self setNeedsLayout];
}
- (UIView *)content {
    if (!_content) {
        _content = [UIView new];
    }
    return _content;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = UIColorHex(#1B1B1B);
    }
    return _titleLabel;
}
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.image = [UIImage imageNamed:@"btn_more_white"];
    }
    return _imageView;
}

@end
